<?php
namespace Wap\Controller;
use Common\Model\UserModel;
use Wap\BaseController;
use Common\Model\EventsModel;
use Common\Model\PointMallModel;
use Common\Model\UtilsModel;
class EventsController extends BaseController {
    //活动首页
    public function index(){
        $uid=session('uid')+0;
        $channel = I('get.channel',0,'intval');
        $fid = I('get.fid',0,'intval');

        $data = EventsModel::getEventsList($uid,1,8,$channel,CLIENT_SHOW_WAP,'',$fid);
        $channel = getChannelKv(OBJ_TYPE_ACTIVITY);
        $this->smarty->assign('eventsChannel',$channel);
        $this->smarty->assign('title',C('SITE_NAME').'-活动列表');
        $this->smarty->assign('data',$data['data']);

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->display("Event_v2/index.html");
    }

    //加载更多
    public function loadMore(){
        $page = I('get.page',2,'intval');
        $channel = I('get.channel',0,'intval');
        $my = I('get.my',0,'intval');
        $uid=session('uid')+0;
        if($my) $data = EventsModel::getMyEventsList($uid,$page,8);
        else $data = EventsModel::getEventsList($uid,$page,8,$channel,CLIENT_SHOW_WAP);


        $this->smarty->assign('data',$data['data']);
        $html = $this->smarty->fetch('Event_v2/more.html');
        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html, 'isPage'=>$data['nextPage']));
    }

    //活动详情
    public function info(){
        $uid=session('uid')+0;
        $id = I("get.id",0,'intval');
        if(!$id) redirect(U('Events/index'));//数据为空返回列表
        $info =EventsModel::getEventsInfo($id,$uid);// response('Events/getEventsInfo', array('id'=>$id));
        if($info['status'] != 0) redirect(U('Events/index'));//数据为空返回列表
        S('EVENTSINFO_'.$id,$info,10);

        if($info['data']['jumpMode']==EventsModel::JUMP_MODE_LIST){
            redirect($info['data']['jumpURL']);
            die;
        }

        if($this->isWechat) {
            $domain=UtilsModel::getFullDomain();
            //微信分享相关
            $this->smarty->assign('isWxShare', true);
            $this->smarty->assign('wxTitle', $info['data']['title']);
            $this->smarty->assign('wxDesc',  $info['data']['shareUri']);
            $this->smarty->assign('wxLink', $domain.U('info?id=' . $id . '&fromUid=' . $uid));
            $this->smarty->assign('wxImage', C('UPLOAD_RESOURCE_PREFIX').$info['data']['imgList']);
            $this->smarty->assign('wxObjType',OBJ_TYPE_ACTIVITY);
            $this->smarty->assign('wxObjID',$id);
        }

        //作业分享
        $data = EventsModel::getTaskShare($id);
        $this->smarty->assign('tasks',$data);

        $askFollow=0;

        $hasFollow=UserModel::checkFollow($uid);
        if(!$hasFollow && M('e_events',null,DB_MAIN_CFG)->where('id='.$id)->getField('mustFollow')==1){
            $askFollow=1;
        }
        $this->smarty->assign('qrcode_path',C('WEIXIN_QRCODE_PATH'));//二维码
        $this->smarty->assign('askFollow',$askFollow);

        $this->smarty->assign('loginuri',U("User/login")."&uri=".urlencode(U("Events/info")."&id=$id"));
        $this->smarty->assign('title',C('SITE_NAME'));//$info['data']['title']
        $this->smarty->assign('info',$info['data']);
        $this->smarty->display("Event_v2/info.html");
    }

    //检查用户作业上交情况
    public function checkUserTask(){
        $uid = session('uid');
        $id = I('get.id',0,'intval');
        $uri = urlencode(U('Events/info')."&id=".$id);
        if(!$uid) redirect(U('User/login')."&uri=".$uri);//数据为空返回列表

        $data = EventsModel::getUserTaskInfo($uid);

        if($data['status'] == 1){
            $this->ajaxReturn($data);
        }
        if(count($data['data']) > 0){
            //有未上交的作业
            $this->ajaxReturn(array('status'=>2,'data'=>$data['data']));
        } else{
            $this->ajaxReturn(array('status'=>0));
        }
    }

    //活动报名界面
    public function apply(){
        $uid = session('uid');

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $id = I('get.id',0,'intval');
        $uri = urlencode(U('Events/info')."&id=".$id);
        if(!$uid) redirect(U('User/login')."&uri=".$uri);//数据为空返回列表

        $info = S('EVENTSINFO_'.$id);
        if(!$info) $info =EventsModel::getEventDetail($id);
        if(!$info) redirect('Events/index');//数据为空返回列表
        $size = C('imgPosition')['IMG_EVENT_APPLY'];
        $info['data']['imgList'] = C('FILE_SHOW_PATH').$size['width'].'x'.$size['height'].substr($info['data']['imgList'],strlen(dirname(dirname($info['data']['imgList']))));

        if($info['status'] == 1) redirect(U('Events/index'));//数据为空返回列表

        $this->smarty->assign('DELIVERY_SEND',PointMallModel::DELIVERY_SEND);
        $this->smarty->assign('DELIVERY_CARD',PointMallModel::DELIVERY_CARD);


        $hasFollow=UserModel::checkFollow($uid);
        if(!$hasFollow && M('e_events',null,DB_MAIN_CFG)->where('id='.$id)->getField('mustFollow')==1){
            $askFollow=1;
        }
        $this->smarty->assign('askFollow',$askFollow);



        $this->smarty->assign('title',C('SITE_NAME').'-活动报名');//$info['data']['title']
        $this->smarty->assign('info',$info['data']);

        $this->smarty->assign('qrcode_path',C('WEIXIN_QRCODE_PATH'));//二维码

        $this->smarty->display("Event_v2/apply.html");
    }

    //活动 确定报名
    public function join(){
        $uid = session('uid');
        $nickname = session('nickname');
        $id = I('get.id',0,'intval');
        $data = array('status'=>1, 'msg'=>'参数错误');
        if(!$uid || !$id) $this->ajaxReturn($data);//数据为空返回列表

        $phone = I('get.phone',null,'strip_tags');
        $realName = I('get.realname',null,'strip_tags');
        $mark = I('get.mark',null,'strip_tags');
        if(!filedPreMatch('phone', $phone)){
            $data['msg'] = '手机号码格式错误';
            $this->ajaxReturn($data);
        }

        if(UtilsModel::strlen($realName)<C('NICKNAME_LENGTH_MIN') || UtilsModel::strlen($realName)>C('NICKNAME_LENGTH_MAX')){
            $data['msg'] = '真实姓名格式错误';
            $this->ajaxReturn($data);
        }

        if($mark && true !== $maskStr = checkCleanWord($mark) ){
            $data['msg'] = '留言中不能包含'.$maskStr;
            $this->ajaxReturn($data);
        }




        //查看是不是绑定手机号码 绑定的手机号为空则需要填写验证码 完成绑定
        if(session('bindPhone') == ''){
            $code = I('get.code',null,'strip_tags');
            $res = UserModel::bindUserPassword($uid, $phone, $code, $realName);
            if($res['status']){
                $this->ajaxReturn($res);
            }else{
                self::resetUserSessions($res['data']);//重新绑定后UID用可能发生变化
                $uid = session('uid');
                $nickname = session('nickname');
            }
        }

        $hasFollow=UserModel::checkFollow($uid);
        if(!$hasFollow && M('e_events',null,DB_MAIN_CFG)->where('id='.$id)->getField('mustFollow')==1){
                $data = array('status'=>2, 'msg'=>'本次活动要求您必须关注本公众号才可参与');//要求关注
                $this->ajaxReturn($data);
        }

        $data =EventsModel::apply($id,$uid,$nickname,$phone,$realName,$mark);
        $data['hasFollow']=$hasFollow;
        $this->ajaxReturn($data);

    }

    //活动报名人员记录
    public function applyLog(){
        $id = I('get.id',0,'intval');
        $type = I('get.type',1,'intval');
        $data = array('status'=>1, 'msg'=>'参数错误');
        if(!$id) $this->ajaxReturn($data);//数据为空返回列表

        $uid = session("uid")+0;
        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('title',C('SITE_NAME'));
        $this->smarty->assign('objType',$type);
        $this->smarty->assign('objID',$id);
        $this->smarty->display("Event_v2/applyLog.html");
    }

    //活动报名人员记录 加载更多
    public function loadMoreLog(){
        $page = I('get.page',1,'intval');
        $id = I('get.id',0,'intval');
        $type = I('get.type',1,'intval');
        $uid = session('uid')+0;
        $data = EventsModel::getEventsApplyListLog($uid,$id,$type,$page,12);

        $this->smarty->assign('uid',$uid);
        $this->smarty->assign('eid',$id);

        $this->smarty->assign('list',$data['data']);
        $this->smarty->assign('hasTask',$data['hasTask']);
        $this->smarty->assign('unjoin',EventsModel::JOIN_STATUS_UNJOIN);
        $this->smarty->assign('noTask',EventsModel::EVENTS_TASK_DONOT);
        $html = $this->smarty->fetch('Event_v2/applyLogList.html');
        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html, 'isPage'=>$data['nextPage']));
    }

    //提交作业页面
    public function pushTask(){
        UtilsModel::checkLogin();
        $uid=session('uid')+0;
        $eid = I('get.eid',0,'intval');
        $this->smarty->assign('eid',$eid);
        $info = EventsModel::getTask($eid,$uid);
        if($info){
            $info['imgList'] = explode(',',$info['imgList']);
        }
        $this->smarty->assign('obj',$info);

        //是否当日首次签到
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('title',"提交作业");
        $this->smarty->display("Events/task.html");
    }
    //提交作业
    public function submitTask(){
        UtilsModel::checkLogin();
        $uid = session('uid')+0;
        $eid = I('get.eid',0,'intval');
        $content = I('get.content',null,'strip_tags');
        $imgList = I('get.imgList',null);
        $imgStr = null;

        $contentLen = count($content);
        if($contentLen<1 || $contentLen >40){
            $this->ajaxReturn(array('status'=>1,'msg'=>'作业内容应控制在1-40个字内，请审核后重新提交！'));
        }

        $imgLen = count($imgList);
        if($imgLen>0 && $imgLen<5){
            $imgStr = implode(',',$imgList);
        }else{
            $this->ajaxReturn(array('status'=>1,'msg'=>'上传图片应1-4张为宜，请审核后重新提交！'));
        }

        $res = EventsModel::saveTask($uid,$eid,$content,$imgStr);
        $this->ajaxReturn($res);
    }

    //取消报名
    public function cancelRegistration(){
        $uid = session('uid');
        $id = I('get.id',0,'intval');
        $data = array('status'=>1, 'msg'=>'参数错误');
        if(!$uid || !$id) $this->ajaxReturn($data);//数据为空返回列表
        $res = EventsModel::cancelRegistration($id,$uid);
        $this->ajaxReturn($res);
    }

}